﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GrayParrot.ServiceBroker.Configuration;

namespace GrayParrot.ServiceBroker
{
    /// <summary>
    /// Publiser class for SQL Service Broker...
    /// </summary>
    public class Publisher : IDisposable
    {
        public string Endpoint { get; set; }
        public string SendTopic { get; set; }
        public string SendTopicDL { get; set; }

        public Publisher()
        {

        }

        public Publisher(string endpoint)
        {
            Endpoint = endpoint;
        }

        public Publisher(Subscription settings)
        {
            Endpoint = ConfigurationManager.ConnectionStrings[settings.ConnectionString].ConnectionString;
            SendTopic = settings.TopicSend;
            SendTopicDL = settings.TopicSendDeadLetter;
        }

        public void Dispose()
        {
            Endpoint = string.Empty;
            SendTopic = string.Empty;
            SendTopicDL = string.Empty;

            GC.SuppressFinalize(this);
        }

        private void Init()
        {
            
        }

        public bool Send(string data)
        {
            bool retValue = false;

            try
            {
                using (SqlConnection conn = new SqlConnection(Endpoint))
                {
                    using (SqlCommand cmd = new SqlCommand(SendTopic, conn))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandTimeout = 600; // seconds
                        SqlParameter m_msg = cmd.Parameters.Add("msg", SqlDbType.VarChar);
                        m_msg.Direction = ParameterDirection.Input;
                        m_msg.Value = data;

                        try
                        {
                            conn.Open();
                            cmd.ExecuteNonQuery();

                            retValue = true;
                        }
                        catch (Exception ex)
                        {
                            retValue = false;
                            GrayParrot.Logging.Logger.For<Publisher>().Error("Errore Publisher", ex);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                GrayParrot.Logging.Logger.For<Publisher>().Error("Errore Publisher", ex);
            }

            return retValue;
        }

        public bool SendToDeadLetter(string data)
        {
            bool retValue = false;

            try
            {
                using (SqlConnection conn = new SqlConnection(Endpoint))
                {
                    using (SqlCommand cmd = new SqlCommand(SendTopicDL, conn))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandTimeout = 600; // seconds
                        SqlParameter m_msg = cmd.Parameters.Add("msg", SqlDbType.VarChar);
                        m_msg.Direction = ParameterDirection.Input;
                        m_msg.Value = data;

                        try
                        {
                            conn.Open();
                            cmd.ExecuteNonQuery();

                            retValue = true;
                        }
                        catch (Exception ex)
                        {
                            retValue = false;
                            GrayParrot.Logging.Logger.For<Publisher>().Error("Errore Publisher for DL", ex);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                GrayParrot.Logging.Logger.For<Publisher>().Error("Errore Publisher for DL", ex);
            }

            return retValue;
        }
    }
}
